package club.dataresources.controller;

import club.dataresources.bo.CustomerBo;
import club.dataresources.common.R;
import club.dataresources.entity.Customer;
import club.dataresources.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author
 * @create 2018-11-12 10:39
 **/
@RequestMapping(value = "/admin/customer")
@Controller
public class AdminCustomerController {
    @Autowired
    private CustomerService customerService;

    @RequestMapping("/addCustomer")
    public @ResponseBody
    R addCustomer(@RequestBody Customer customer){
        System.out.println("==================增加新顾客===================");
        try {
            if(customerService.addCustomer(customer)){
                return R.ok().put("code",200).put("msg","新增成功");
            }else{
                return R.error().put("code",500).put("msg","新增失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    @RequestMapping("/removeCustomer")
    public @ResponseBody
    R removeCustomer(@RequestParam("id") Integer id){
        System.out.println("==================删除顾客===================");
        try {
            if(customerService.removeCustomer(id)){
                return R.ok().put("code",200).put("msg","删除成功");
            }else{
                return R.error().put("code",500).put("msg","删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    @RequestMapping("/updateCustomer")
    public @ResponseBody
    R updateCustomer(@RequestBody Customer customer){
        System.out.println("==================更新顾客===================");
        try {
            if(customerService.updateCustomer(customer)){
                return R.ok().put("code",200).put("msg","更新成功");
            }else{
                return R.error().put("code",500).put("msg","更新失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }


    @RequestMapping("/listCustomer")
    public @ResponseBody
    R listCustomer(@RequestBody CustomerBo customerBo){
        System.out.println("==================查询顾客===================");
        try {
            if(customerService.updateCustomer(customerBo)){
                return R.ok().put("code",200).put("msg","查询完毕");
            }else{
                return R.error().put("code",500).put("msg","查询失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }
}
